/**
 * Created by Admin on 2018/1/10.
 */
define(function (require, exports, module) {
    var commonPaging = require("../common/commonPagingCq.js");
    var tpl = require("./tpl/patSearchTpl.html");
    var template = require("template");

    require("../common/dialog.js");
    require("../common/My97DatePicker1/WdatePicker.js");
    require("../common/qy-template");
    require("../seajs-init");


    // var type = localStorage["type"];

    init()

    var p;
    setTimeout(function () {
        // if (type != "") {
        //     p = new commonPaging({
        //         htmlContainer: "#Js_page_content",
        //         pageContainer: "#Js_fs_paging",
        //         url: window.jkconfig.webapiurlv + "analysis/patentSearch/search",
        //         tpl: tpl,
        //         data: getSearchData() + "&patType=" + type
        //     });
        //     p.init();
        //
        // } else {
        //     p = new commonPaging({
        //         htmlContainer: "#Js_page_content",
        //         pageContainer: "#Js_fs_paging",
        //         url: window.jkconfig.webapiurlv + "analysis/patentSearch/search",
        //         tpl: tpl,
        //         data: getSearchData()
        //     });
        //     p.init();
        // }
        p = new commonPaging({
            htmlContainer: "#Js_page_content",
            pageContainer: "#Js_fs_paging",
            url: window.jkconfig.webapiurlv + "analysis/patentSearch/search",
            tpl: tpl,
            data: getSearchData()
        });
        p.init();
    }, 1000)


    $(function () {
        $("body").on("click", ".btnSearch", function () {
            /*申请号、专利名称、权利人、发明人、代理机构、地址*/
            $("[name=patNum]").val($.trim($("[name=patNum]").val()));
            $("[name=patName]").val($.trim($("[name=patName]").val()));
            $("[name=applyPerson]").val($.trim($("[name=applyPerson]").val()));
            $("[name=inventPerson]").val($.trim($("[name=inventPerson]").val()));
            $("[name=agencyName]").val($.trim($("[name=agencyName]").val()));
            $("[name=address]").val($.trim($("[name=address]").val()));
            searchFn();
        }).on("click", ".btnExport", function () {
            // 导出
            window.location.href = window.jkconfig.webapiurlv + "analysis/patentSearch/searchexport" + "?" + getSearchData() + "&pageSize=5000&" + api_key + "=" + token;
        }).on("click", ".ptmt-curpointer", function () {
            // 排序
            if ($(this).children(".up").length > 0) {
                $(this).children(".up").removeClass("up").addClass("down");
                $("[name=sort]").val("ASC");
                $("[name=orderby]").val($(this).attr("data-sort"));
                searchFn();
            } else {
                $(this).children(".down").removeClass("down").addClass("up");
                $("[name=sort]").val("DESC");
                $("[name=orderby]").val($(this).attr("data-sort"));
                searchFn();
            }
        })
        localStorage["agencyName"] = "";
        // localStorage["type"]="";
    })

    function searchFn() {
        if ($("#Js_fs_paging").css("display") == "block") {
            p && p.page && p.page.destroy && p.page.destroy();
            $("[name=pageNumber]").val(1);
            p = new commonPaging({
                htmlContainer: "#Js_page_content",
                pageContainer: "#Js_fs_paging",
                url: window.jkconfig.webapiurlv + "analysis/patentSearch/search",
                tpl: tpl,
                data: getSearchData()
            });
            p.init();
        } else {
            p = new commonPaging({
                htmlContainer: "#Js_page_content",
                pageContainer: "#Js_fs_paging",
                url: window.jkconfig.webapiurlv + "analysis/patentSearch/search",
                tpl: tpl,
                data: $("#searchForm").serialize()
            });
            p.init();
        }
    }

    function getSearchData() {
        return $("#searchForm").serialize()
    }


    function init() {
        var applyPerson = localStorage["applyPerson"];
        var agencyName = localStorage["agencyName"];
        var patHubTypeName = localStorage["patHubTypeName"]
        var lawState = localStorage['lawState']
        var patAppDateBegin = localStorage['patAppDateBegin']
        var patAppDateEnd = localStorage['patAppDateEnd']
        var patApplyDateBegin = localStorage['patApplyDateBegin']
        var patApplyDateEnd = localStorage['patApplyDateEnd']
        // var areaId = localStorage['areaId']
        applyPerson && $("#applyPerson").val(applyPerson);
        agencyName && $("#agencyName").val(agencyName);

        if (patHubTypeName) {
            patHubTypeName = JSON.parse(patHubTypeName);
            if (patHubTypeName && patHubTypeName.length > 0) {
                $('[name=patHubTypeName]').prop('checked', false);
                patHubTypeName.forEach(function (v) {
                    $('[name=patHubTypeName][value=' + v + ']').prop('checked', true)
                })
            }
        }

        if (lawState) {
            lawState = JSON.parse(lawState)
            if (lawState && lawState.length > 0) {
                $('[name=lawState]').prop('checked', false);
                lawState.forEach(function (v) {
                    $('[name=lawState][value=' + v + ']').prop('checked', true)
                })
            }
        }
        // 申请日
        if (patApplyDateBegin) {
            $('[name=shenqingriStart]').val(patApplyDateBegin)
        }
        if (patApplyDateEnd) {
            $('[name=shenqingriEnd]').val(patApplyDateEnd)
        }


        //授权日
        if (patAppDateBegin) {
            $('[name=shouquanriStart]').val(patAppDateBegin)
        }
        if (patAppDateEnd) {
            $('[name=shouquanriEnd]').val(patAppDateEnd)
        }
        if (areaId) {
            $('#areaId').val(areaId)
        }
        localStorage.removeItem('agencyName');
        localStorage.removeItem('applyPerson');
        localStorage.removeItem('patHubTypeName');
        localStorage.removeItem('lawState');
        localStorage.removeItem('patAppDateBegin');
        localStorage.removeItem('patAppDateEnd');
        localStorage.removeItem('patApplyDateBegin');
        localStorage.removeItem('patApplyDateEnd');
        // localStorage.removeItem('areaId')
    }
})